% 
%  PySL:  Use S-Lang from Python (and vice versa)
%  Copyright (C) 2005,2006 Christopher J. Stawarz <chris@pseudogreen.org>
% 
%  This program is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2 of the License, or
%  (at your option) any later version.
% 
%  This program is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
% 
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software
%  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
% 


import("pysl");


%
% Add current directory to search path
%

py_import("sys");
if (py.sys.path[0] != "")
    py_call(py.sys.path.insert, 0, "");


%
% Make Python's help() easily accessible
%

define py_help() {
    variable args = __pop_args(_NARGS);
    py_call(py.help, __push_args(args));
}


%
% If we're running in slsh, redefine print() so that it calls Python's
% str() for PyObjects
%

#ifexists prepend_to_slang_load_path
% Keep the original print() in the _pysl namespace
() = evalfile("print", "_pysl");

define print(x) {
    if (typeof(x) == PyObject)
        () = fprintf(stdout, "%s\n", py_call(py.str, x));
    else
        _pysl->print(x);
}
#endif


%
% Map the standard Python exceptions to S-Lang errors
%

py2sl_errormap[py.Exception] = AnyError;
%py2sl_errormap[py.SystemExit] = ;
%py2sl_errormap[py.StopIteration] = ;
py2sl_errormap[py.StandardError] = AnyError;
py2sl_errormap[py.KeyboardInterrupt] = UserBreakError;
py2sl_errormap[py.ImportError] = ImportError;
py2sl_errormap[py.EnvironmentError] = OSError;
py2sl_errormap[py.IOError] = IOError;
py2sl_errormap[py.OSError] = OSError;
if (_py_call(py.getattr, py, "WindowsError", NULL) != NULL)
    py2sl_errormap[py.WindowsError] = OSError;
py2sl_errormap[py.EOFError] = ReadError;
py2sl_errormap[py.RuntimeError] = RunTimeError;
py2sl_errormap[py.NotImplementedError] = NotImplementedError;
py2sl_errormap[py.NameError] = UndefinedNameError;
py2sl_errormap[py.UnboundLocalError] = VariableUninitializedError;
py2sl_errormap[py.AttributeError] = InvalidParmError;
py2sl_errormap[py.SyntaxError] = SyntaxError;
py2sl_errormap[py.IndentationError] = SyntaxError;
py2sl_errormap[py.TabError] = SyntaxError;
py2sl_errormap[py.TypeError] = TypeMismatchError;
%py2sl_errormap[py.AssertionError] = ;
py2sl_errormap[py.LookupError] = IndexError;
py2sl_errormap[py.IndexError] = IndexError;
py2sl_errormap[py.KeyError] = IndexError;
py2sl_errormap[py.ArithmeticError] = MathError;
py2sl_errormap[py.OverflowError] = ArithOverflowError;
py2sl_errormap[py.ZeroDivisionError] = DivideByZeroError;
py2sl_errormap[py.FloatingPointError] = MathError;
py2sl_errormap[py.ValueError] = DataError;
py2sl_errormap[py.UnicodeError] = UnicodeError;
py2sl_errormap[py.UnicodeEncodeError] = UnicodeError;
py2sl_errormap[py.UnicodeDecodeError] = UnicodeError;
py2sl_errormap[py.UnicodeTranslateError] = UnicodeError;
%py2sl_errormap[py.ReferenceError] = ;
py2sl_errormap[py.SystemError] = InternalError;
py2sl_errormap[py.MemoryError] = MallocError;


%
% Register package
%

#ifexists provide
provide("pysl");
#endif
